package com.lsk.util;

import org.apache.log4j.Logger;
import sun.security.ssl.Debug;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * @Description 日志记录类
 * @author panteng
 * @version V0.0.1
 * @date 2016-09-08
 */
public class Syslog {


        public static Logger log = Logger.getLogger(Syslog.class);

        /**
         * 打印警告
         *
         * @param obj
         */
        public static void warn(Object obj) {
            try{
                /*** 获取输出信息的代码的位置 ***/
                String location = "";
                StackTraceElement[] stacks = Thread.currentThread().getStackTrace();
                location = stacks[2].getClassName() + "." + stacks[2].getMethodName()
                        + "(" + stacks[2].getLineNumber() + ")";
                /*** 是否是异常  ***/
                if (obj instanceof Exception) {
                    Exception e = (Exception) obj;
                    StringWriter sw = new StringWriter();
                    e.printStackTrace(new PrintWriter(sw, true));
                    String str = sw.toString();
                    log.warn(location + str);
                } else {
                    log.warn(location + obj.toString());
                }
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    public static void debug(Object obj) {
        try{
            /*** 获取输出信息的代码的位置 ***/
            String location = "";
            StackTraceElement[] stacks = Thread.currentThread().getStackTrace();
            location = stacks[2].getClassName() + "." + stacks[2].getMethodName()
                    + "(" + stacks[2].getLineNumber() + ")";
            /*** 是否是异常  ***/
            if (obj instanceof Exception) {
                Exception e = (Exception) obj;
                StringWriter sw = new StringWriter();
                e.printStackTrace(new PrintWriter(sw, true));
                String str = sw.toString();
                log.debug(" SQL>> "+ str);
            } else {
                log.debug(" SQL>> " + obj.toString());
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
        /**
         * 打印信息
         *
         * @param obj
         */
        public static void info(Object obj) {
            try{
                /*** 获取输出信息的代码的位置 ***/
                String location = "";
                StackTraceElement[] stacks = Thread.currentThread().getStackTrace();
                location = stacks[2].getClassName() + "." + stacks[2].getMethodName()
                        + "(" + stacks[2].getLineNumber() + ")";
                /*** 是否是异常  ***/
                if (obj instanceof Exception) {
                    Exception e = (Exception) obj;
                    StringWriter sw = new StringWriter();
                    e.printStackTrace(new PrintWriter(sw, true));
                    String str = sw.toString();
                    log.info(location + str);
                } else {
                    log.info(location + obj.toString());
                }
            }catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }

        /**
         * 打印错误
         *
         * @param obj
         */
        public static void error(Object obj) {
            try{
                /*** 获取输出信息的代码的位置 ***/
                String location = "";
                StackTraceElement[] stacks = Thread.currentThread().getStackTrace();
                location = stacks[2].getClassName() + "." + stacks[2].getMethodName()
                        + "(" + stacks[2].getLineNumber() + ")";

                /*** 是否是异常  ***/
                if (obj instanceof Exception) {
                    Exception e = (Exception) obj;
                    StringWriter sw = new StringWriter();
                    e.printStackTrace(new PrintWriter(sw, true));
                    String str = sw.toString();
                    log.error(location + str);
                } else {
                    log.error(location + obj.toString());
                }
            }catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }

     /*   *//**
         * 向数据库告警表中插入信息
         * @param obj
         *//*
        public staticResource void dbWarn(Object obj) {
            try{
                String printInfo = "";
                *//*** 获取输出信息的代码的位置 ***//*
                String location = "";
                StackTraceElement[] stacks = Thread.currentThread().getStackTrace();
                location = stacks[2].getClassName() + "." + stacks[2].getMethodName()
                        + "(" + stacks[2].getLineNumber() + ")";

                *//*** 是否是异常  ***//*
                if (obj instanceof Exception) {
                    Exception e = (Exception) obj;
                    printInfo = location + e.getMessage();
                    log.fatal(printInfo.substring(0, printInfo.length() > 512?512:printInfo.length()));
                } else {
                    printInfo = location + obj.toString();
                    log.fatal(printInfo.substring(0, printInfo.length() > 512?512:printInfo.length()));
                }
            }catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }*/

        /**
         * 获取调用此函数的代码的位置
         * @return 包名.类名.方法名(行数)
         */
        public static String getCodeLocation(){
            try{
                /*** 获取输出信息的代码的位置 ***/
                String location = "";
                StackTraceElement[] stacks = Thread.currentThread().getStackTrace();
                location = stacks[2].getClassName() + "." + stacks[2].getMethodName()
                        + "(" + stacks[2].getLineNumber() + ")";
                return location;
            }catch (Exception e) {
                // TODO: handle exception
                Syslog.error(e);
                return "";
            }
        }
    }

